import amqp from 'amqplib';
const rabbitMQServer = "amqp://mingcai_mq:EUDNfpZRbhfjb8PZ@95.169.25.181";

async function startConsumer() {
    const connection = await amqp.connect(rabbitMQServer); // RabbitMQ 服务器地址
    const channel = await connection.createChannel();

    const exchange = 'topic_exchange'; // Topic Exchange 名称
    const queue = 'user_notification_queue'; // 队列名称

    // 声明 Exchange 和 Queue
    await channel.assertExchange(exchange, 'topic', { durable: false });
    await channel.assertQueue(queue, { durable: false });

    // 绑定 Queue 到 Exchange，并指定订阅的 Routing Key
    await channel.bindQueue(queue, exchange, 'order.created');
    await channel.bindQueue(queue, exchange, 'payment.success');

    // 打印接收到的消息
    channel.consume(queue, (msg) => {
        if (msg?.content) {
            console.log(`Received message: ${msg.content.toString()}`);
        }
    }, { noAck: true });

    console.log('User Notification Module is listening for "order.created" and "payment.success" messages...');
}

startConsumer();
